//
//  JoinGrpValidateViewController.m
//  SouthChina
//
//  Created by kc on 15/7/30.
//  Copyright (c) 2015年 科创. All rights reserved.
//邀请入群验证

#import "JoinGrpValidateViewController.h"
#import "UnitView.h"
#import "DataDictDao.h"

@interface JoinGrpValidateViewController()<UnitViewDelegate,UIAlertViewDelegate>
{}
@property (strong, nonatomic) IBOutlet UIImageView *GrpImage;//群头像
@property (strong, nonatomic) IBOutlet UILabel *GrpName;//群名称

@property (strong, nonatomic) IBOutlet UILabel *GrpTags;//群标签
@property (strong, nonatomic) IBOutlet UnitView *GrpUsers;//群成员
@property (strong, nonatomic) NSMutableSet *groupuserarr;//群成员
@end

@implementation JoinGrpValidateViewController
@synthesize groupInfoDic,onOperateListener,message;

//将dictdataname 转换为 dictdataValue
-(NSString *) getlabelname:(NSString *)dictdataId
{
    if ([dictdataId isEqualToString:@""]) {
        return @"";
    }
    else
    {   //80,81, 转换为 ‘80’,'81'
        NSArray *list=[dictdataId componentsSeparatedByString:@","];
        NSMutableString *skeylist = [[NSMutableString alloc] initWithString:@""];
        NSMutableString *sreturnlist = [[NSMutableString alloc] initWithString:@""];
        for(NSInteger i = 0;i<list.count;i++)
        {
            if(![list[i] isEqualToString:@""])
            {
                if(i==0)
                    [skeylist insertString:[NSString stringWithFormat:@"'%@'",list[i]] atIndex:0];
                else
                    [skeylist insertString:[NSString stringWithFormat:@"'%@',",list[i]] atIndex:0];
            }
            
        }
        NSLog(@"sreturnlist:%@",skeylist);
        NSString *sql = [NSString stringWithFormat:@"where dicttypeId ='8' and dictdataName in (%@)",skeylist];
        NSMutableArray *allLabeData = [DataDictDao queryDataDict:sql];
        for(NSInteger i = 0;i<allLabeData.count;i++)
        {
            DataDict * dataDict = [allLabeData objectAtIndex:i];
            if(i==0)
                [sreturnlist insertString:[NSString stringWithFormat:@"%@",dataDict.dictdataValue] atIndex:0];
            else
                [sreturnlist insertString:[NSString stringWithFormat:@"%@,",dataDict.dictdataValue] atIndex:0];
        }
        NSLog(@"数据库转换为：%@",sreturnlist);
        return  sreturnlist;
    }
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
     [self addTitleBarWithBackBtn:@"群资料" needReturn:YES];
    
    UIView *bodyView = [[[NSBundle mainBundle] loadNibNamed:@"JoinGrpValidateBody" owner:self options:nil] objectAtIndex:0];
    bodyView.frame = (CGRect){0, TITLE_BAR_HEGIHT+YNEW,SCREEN_WIDTH, SCREEN_HEIGHT-TITLE_BAR_HEGIHT-YNEW};
    [self.view addSubview:bodyView];
    
    _GrpUsers = [_GrpUsers initWithFrame:_GrpUsers.frame];
    _GrpUsers.isDeleteable = NO;
    _GrpUsers.delegate = self;
    
    //群头像（暂时没有群头像）
    /*NSURL *url= [NSURL URLWithString: [[groupInfoDic objectForKey:@"headImg"] hasPrefix:@"http"]?[content objectForKey:@"headImg"]:[NSString stringWithFormat:@"%@%@",DE.fileEngineeUrl,[content objectForKey:@"headImg"]]];
    */
    //群头像
    _GrpImage.image = [UIImage imageNamed:@"group"];
    //群名称
    _GrpName.text = [groupInfoDic objectForKey:@"groupName"];
    
    //根据群ID查询陌生群信息
    _groupuserarr  = [[NSMutableSet alloc] init];
    NSDictionary *map = @{@"groupId":[groupInfoDic objectForKey:@"groupId"]};
    //陌生群只返回管理员以上人员，其他人员不显示
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    [DE serverCall:@"queryStrangeGrpInfo" params:map callback:^(BOOL success, int code, NSString *desc, NSDictionary *data,NSDictionary*param) {
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        if(success)
        {
            //群信息
            NSMutableDictionary *dictionary = [data objectForKey:@"userGroup"];
            NSString *stags = [dictionary objectForKey:@"groupTags"]==nil?@"":[dictionary objectForKey:@"groupTags"];
            //查询数据字典，将数字转换为汉字0724
            _GrpTags.text = [[NSString alloc] initWithFormat:@"%@" ,[self getlabelname:stags]];
            //群成员
            NSMutableSet *userarr = [data objectForKey:@"members"];
            NSEnumerator *enumerator = [userarr objectEnumerator];
            
            NSMutableDictionary *dictionarytmp  = [enumerator nextObject];
            while (dictionarytmp != nil) {
                NSLog(@"memberId:%@",[dictionarytmp objectForKey:@"memberId"]);
                Contact *contact = [[Contact alloc] init];
                contact.mID = [dictionarytmp objectForKey:@"memberId"];
                contact.mHEAD_IMG = [dictionarytmp objectForKey:@"headImg"];
                [_groupuserarr addObject:contact];
                NSLog(@"%lu",_groupuserarr.count);
                dictionarytmp  = [enumerator nextObject];
            }
            for(Contact *contact in _groupuserarr)
            {
                [_GrpUsers addNewUnit:contact];
            }
        }
        else{
            [self showAlerHUD:@"查询群信息失败！"];
        }
    }];
    
    //同意入群按钮
    UIImage *img=[UIImage imageNamed:@"blue_btn_normal.png"];
    img=[img stretchableImageWithLeftCapWidth:20 topCapHeight:20];
    UIButton * addGroupBtn = [[UIButton alloc] initWithFrame:CGRectMake(15,_GrpUsers.frame.origin.y+100, (SCREEN_WIDTH)-30, 40)];
    [addGroupBtn setBackgroundImage:img forState:UIControlStateNormal];
    [addGroupBtn addTarget:self action:@selector(addGroupBtn:) forControlEvents:UIControlEventTouchUpInside];
    [addGroupBtn setTitle:@"同意入群" forState:UIControlStateNormal];
    [ bodyView addSubview:addGroupBtn];
    
    //拒绝按钮red_btn_normal
    //UIImage *img0=[UIImage imageNamed:@"red_btn_normal.png"];
    UIButton * refusedBtn = [[UIButton alloc] initWithFrame:CGRectMake(15,addGroupBtn.frame.origin.y+70, (SCREEN_WIDTH)-30, 40)];
    //[refusedBtn setBackgroundImage:img0 forState:UIControlStateNormal];
    [refusedBtn setBackgroundColor:[UIColor redColor]];
    [refusedBtn addTarget:self action:@selector(refusedAddGrpBtn:) forControlEvents:UIControlEventTouchUpInside];
    [refusedBtn setTitle:@"拒绝" forState:UIControlStateNormal];
    [ bodyView addSubview:refusedBtn];
}

//同意入群
-(void)addGroupBtn:(id)sender
{
    NSLog(@"同意入群");
    [self.navigationController popViewControllerAnimated:NO];
    
    [onOperateListener onOperateButtonClick:message acceptOrNot:YES];
    
}

//拒绝入群
-(void)refusedAddGrpBtn:(id)sender
{
    [self.navigationController popViewControllerAnimated:NO];
        [onOperateListener onOperateButtonClick:message acceptOrNot:NO];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

//点击单元事件
- (void) extendCellTouched:(UnitCell *)unitCell
{
    NSLog(@"展示群成员");
    /*ShowMemberViewController *showusertalbe = [[ShowMemberViewController alloc] init];
    showusertalbe.curOrgContact = contact;
    showusertalbe.CreateType = SHOW_USER;
    [kAppDelegate.iCSDrawerController.navigationController pushViewController:showusertalbe animated:NO];*/
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
